package com.ruoyi.web.controller.basicInformation;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.TViolationTypeTree;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.basicInformation.vo.TViolationTypeVo;
import com.ruoyi.system.basicInformation.bo.TViolationTypeQueryBo;
import com.ruoyi.system.basicInformation.bo.TViolationTypeAddBo;
import com.ruoyi.system.basicInformation.bo.TViolationTypeEditBo;
import com.ruoyi.system.basicInformation.service.ITViolationTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 违法类型Controller
 * 
 * @author ruoyi
 * @date 2021-06-01
 */
@Api(value = "违法类型控制器", tags = {"违法类型管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/basicInformation/violationType")
public class TViolationTypeController extends BaseController {

    private final ITViolationTypeService iTViolationTypeService;

    /**
     * 查询违法类型列表
     */
    @ApiOperation("查询违法类型列表")
    @PreAuthorize("@ss.hasPermi('basicInformation:violationType:list')")
    @GetMapping("/list")
    public AjaxResult<List<TViolationTypeVo>> list(TViolationTypeQueryBo bo) {
        List<TViolationTypeVo> list = iTViolationTypeService.queryList(bo);
        return AjaxResult.success(list);
    }

    /**
     * 导出违法类型列表
     */
    @ApiOperation("导出违法类型列表")
    @PreAuthorize("@ss.hasPermi('basicInformation:violationType:export')")
    @Log(title = "违法类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<TViolationTypeVo> export(TViolationTypeQueryBo bo) {
        List<TViolationTypeVo> list = iTViolationTypeService.queryList(bo);
        ExcelUtil<TViolationTypeVo> util = new ExcelUtil<TViolationTypeVo>(TViolationTypeVo.class);
        return util.exportExcel(list, "违法类型");
    }

    /**
     * 获取违法类型详细信息
     */
    @ApiOperation("获取违法类型详细信息")
    @PreAuthorize("@ss.hasPermi('basicInformation:violationType:query')")
    @GetMapping("/{id}")
    public AjaxResult<TViolationTypeVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iTViolationTypeService.queryById(id));
    }

    /**
     * 新增违法类型
     */
    @ApiOperation("新增违法类型")
    @PreAuthorize("@ss.hasPermi('basicInformation:violationType:add')")
    @Log(title = "违法类型", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody TViolationTypeAddBo bo) {
//        if ("1".equals(iTViolationTypeService.checkUserNameUnique(bo.getName()))) {
//            return AjaxResult.error("新增类型'" + bo.getName() + "'失败，类型已存在");
//        }
        return toAjax(iTViolationTypeService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改违法类型
     */
    @ApiOperation("修改违法类型")
    @PreAuthorize("@ss.hasPermi('basicInformation:violationType:edit')")
    @Log(title = "违法类型", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody TViolationTypeEditBo bo) {
//        if ("1".equals(iTViolationTypeService.checkUserNameUnique(bo.getName()))) {
//            return AjaxResult.error("修改类型'" + bo.getName() + "'失败，类型已存在");
//        }
        return toAjax(iTViolationTypeService.updateByEditBo(bo) ? 1 : 0);
    }

    @GetMapping("/treeselect")
    public AjaxResult treeselect(TViolationTypeTree dept)
    {
        List<TViolationTypeTree> depts = iTViolationTypeService.selectViolationTypeList(dept);
        return AjaxResult.success(iTViolationTypeService.buildViolationTypeTreeSelect(depts));
    }

    /**
     * 删除违法类型
     */
    @ApiOperation("删除违法类型")
    @PreAuthorize("@ss.hasPermi('basicInformation:violationType:remove')")
    @Log(title = "违法类型" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        return toAjax(iTViolationTypeService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
